package com.ssi.myshop.ui;


import android.view.KeyEvent;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.ssi.myshop.R;
import com.ssi.myshop.base.BaseActivity;
import com.ssi.myshop.base.BaseFragment;
import com.ssi.myshop.ui.fragment.CarFragment;
import com.ssi.myshop.ui.fragment.CategoryFragment;
import com.ssi.myshop.ui.fragment.HomeFragment;

import butterknife.BindView;


public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
    private static final String TAG = "MainActivity";

    @BindView(R.id.home_navigation_bar)
    RadioGroup mGroup;

    private long firstTime = 0;
    private HomeFragment mHomeFragment;
    private FragmentManager mFm;
    private CategoryFragment mCategoryFragment;
    private CarFragment mCarFragment;


    @Override
    protected void initPresent() {

    }

    protected void initEvent() {

    }

    @Override
    protected void initView() {
        mGroup.setOnCheckedChangeListener(this);
        mHomeFragment = new HomeFragment();
        mCategoryFragment = new CategoryFragment();
        mCarFragment = new CarFragment();
        mFm = getSupportFragmentManager();
        switchFragment(mHomeFragment);
    }

    private BaseFragment lastFragment = null;

    private void switchFragment(BaseFragment targetFragment) {
        if (lastFragment == targetFragment) {
            return;
        }

        FragmentTransaction fragmentTransaction = mFm.beginTransaction();
        if (!targetFragment.isAdded()) {
            fragmentTransaction.add(R.id.main_page_container, targetFragment);
        } else {
            fragmentTransaction.show(targetFragment);
        }

        if (lastFragment != null) {
            fragmentTransaction.hide(lastFragment);
        }

        lastFragment = targetFragment;

        fragmentTransaction.commit();

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            long currentTimeMillis = System.currentTimeMillis();
            if (currentTimeMillis - firstTime > 2000) {
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                firstTime = currentTimeMillis;
                return true;
            } else {
                System.exit(0);
            }
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_home:
                switchFragment(mHomeFragment);
                break;
            case R.id.rb_category:
                switchFragment(mCategoryFragment);
                break;
            case R.id.rb_car:
                switchFragment(mCarFragment);
                break;
        }
    }
}
